package com.assets.Check;

import android.app.Dialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.assets.R;
import com.assets.bean.Input_item1;
import com.assets.databinding.InputAdapterItemBinding;
import com.assets.newNHF.util.LogUtils;
import com.assets.util.RequestResult;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * @作者(author): WR
 * @创建时间(date): 2022/3/4
 */
public class CheckAdapter extends RecyclerView.Adapter<CheckAdapter.CheckViewHolder> {
    private List<Input_item1> inputItem1List;
    private Dialog dialog;

    private RequestResult<Integer> requestResult;
    public CheckAdapter(List<Input_item1> inputItem1List) {
        this.inputItem1List = inputItem1List;
    }

    public void setRequestSuccess(RequestResult<Integer> requestResult){
        this.requestResult=requestResult;
    }
    @NonNull
    @Override
    public CheckViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        InputAdapterItemBinding binding = InputAdapterItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new CheckViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull CheckViewHolder holder, int position) {
        Input_item1 linkeMain = inputItem1List.get(position);
        holder.mainBinding.AssetName.setText(linkeMain.getAsset_name());
        holder.mainBinding.Section.setText(linkeMain.getSection());
        holder.mainBinding.CodeNumber.setText(linkeMain.getCode_number());
//        holder.mainBinding.Custodian.setText(linkeMain.getCustodian());
        holder.mainBinding.Model.setText(linkeMain.getModel());
        holder.mainBinding.rfidTv.setText(linkeMain.getRfid());
        holder.mainBinding.Category.setText(linkeMain.getCategory());
        String image = linkeMain.getAsset_img();
//        if (!TextUtils.isEmpty(image)) {
//            Glide.with(holder.itemView.getContext()).load(image).into(holder.mainBinding.AssetImg);
//        }
//        holder.mainBinding.tvReport.setOnClickListener(v->{
//            if (requestResult!=null){
//                requestResult.result(position);
//            }
//
//        });
//        holder.mainBinding.AssetImg.setOnClickListener(v -> {
//            if (TextUtils.isEmpty(image)) {
//                LogUtils.d(image);
//                return;
//            }
//            //展示在dialog上面的大图
//            dialog = new Dialog(holder.itemView.getContext(), R.style.FullActivity);
//            WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
//            attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
//            attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
//            dialog.getWindow().setAttributes(attributes);
//            ImageView imageView = new ImageView(holder.itemView.getContext());
//            imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//            dialog.setContentView(imageView);
//            Glide.with(holder.itemView.getContext()).load(image).into(imageView);
//            //大图的点击事件（点击让他消失）
//            dialog.show();
//            imageView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    dialog.dismiss();
//                }
//            });
//        });
    }

    @Override
    public int getItemCount() {
        return inputItem1List.size();
    }

    public interface onClick {
        void result(Integer position);
    }

    private onClick onClick;

    public void setOnClick(CheckAdapter.onClick onClick) {
        this.onClick = onClick;
    }

    class CheckViewHolder extends RecyclerView.ViewHolder {
        private InputAdapterItemBinding mainBinding;

        public CheckViewHolder(@NonNull InputAdapterItemBinding binding) {
            super(binding.getRoot());
            mainBinding = binding;
            binding.getRoot().setOnClickListener(v -> {
                if (onClick != null) {
                    onClick.result(getAdapterPosition());
                }
            });
        }
    }


}
